package cn.zzdt4j.config.model.biz.impl;

import cn.hutool.json.JSONUtil;
import cn.zzdt4j.common.extension.enums.VerifyEnum;
import cn.zzdt4j.common.toolkit.BeanUtil;
import cn.zzdt4j.common.toolkit.UserContext;
import cn.zzdt4j.config.mapper.HisConfigVerifyMapper;
import cn.zzdt4j.config.model.HisConfigVerifyInfo;
import cn.zzdt4j.config.model.biz.threadpool.ConfigModifySaveReqDTO;
import cn.zzdt4j.config.model.biz.threadpool.ConfigModifyVerifyReqDTO;
import cn.zzdt4j.config.service.biz.ConfigModificationVerifyService;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;

import javax.annotation.Resource;
import java.util.Date;

/**
 * The type Abstract config modification verify service.
 *
 * @author by <a href="mailto:ligang941012@gmail.com">gang.Li</a>
 * @since 2023 /11/12 21:27
 */
public abstract class AbstractConfigModificationVerifyService implements ConfigModificationVerifyService {

    /**
     * The His config verify mapper.
     */
    @Resource
    protected HisConfigVerifyMapper hisConfigVerifyMapper;


    @Override
    public void saveConfigModifyApplication(ConfigModifySaveReqDTO reqDTO) {
        final HisConfigVerifyInfo hisConfigVerifyInfo = BeanUtil.convert(reqDTO, HisConfigVerifyInfo.class);
        hisConfigVerifyInfo.setContent(JSONUtil.toJsonStr(reqDTO));
        hisConfigVerifyInfo.setVerifyStatus(VerifyEnum.TO_VERIFY.getVerifyStatus());
        hisConfigVerifyMapper.insert(hisConfigVerifyInfo);
    }

    @Override
    public void rejectModification(ConfigModifyVerifyReqDTO reqDTO) {
        updateWrapper(reqDTO, VerifyEnum.VERIFY_REJECT);
    }

    @Override
    public void acceptModification(ConfigModifyVerifyReqDTO reqDTO) {
        updateThreadPoolParameter(reqDTO);
        updateWrapper(reqDTO, VerifyEnum.VERIFY_ACCEPT);
        final Date gmtCreate = hisConfigVerifyMapper.selectById(reqDTO.getId()).getGmtCreate();
        final LambdaUpdateWrapper<HisConfigVerifyInfo> invalidUpdateWrapper = Wrappers.lambdaUpdate(HisConfigVerifyInfo.class)
                .eq(HisConfigVerifyInfo::getType, reqDTO.getType())
                .eq(reqDTO.getTenantId() != null, HisConfigVerifyInfo::getTenantId, reqDTO.getTenantId())
                .eq(reqDTO.getItemId() != null, HisConfigVerifyInfo::getItemId, reqDTO.getItemId())
                .eq(reqDTO.getTpId() != null, HisConfigVerifyInfo::getTpId, reqDTO.getTpId())
                .eq(reqDTO.getIdentify() != null, HisConfigVerifyInfo::getIdentify, reqDTO.getIdentify())
                .lt(HisConfigVerifyInfo::getGmtCreate, gmtCreate)
                .eq(HisConfigVerifyInfo::getVerifyStatus, VerifyEnum.TO_VERIFY.getVerifyStatus())
                .set(HisConfigVerifyInfo::getVerifyStatus, VerifyEnum.VERIFY_INVALID.getVerifyStatus())
                .set(HisConfigVerifyInfo::getVerifyUser, UserContext.getUserName())
                .set(HisConfigVerifyInfo::getGmtVerify, new Date());
        hisConfigVerifyMapper.update(null, invalidUpdateWrapper);
    }


    private void updateWrapper(ConfigModifyVerifyReqDTO reqDTO, VerifyEnum verifyReject) {
        final LambdaUpdateWrapper<HisConfigVerifyInfo> updateWrapper = Wrappers.lambdaUpdate(HisConfigVerifyInfo.class)
                .eq(HisConfigVerifyInfo::getId, Long.parseLong(reqDTO.getId()))
                .set(HisConfigVerifyInfo::getVerifyStatus, verifyReject.getVerifyStatus())
                .set(HisConfigVerifyInfo::getGmtVerify, new Date())
                .set(HisConfigVerifyInfo::getVerifyUser, UserContext.getUserName());
        hisConfigVerifyMapper.update(null, updateWrapper);
    }

    /**
     * Update thread pool parameter.
     *
     * @param reqDTO the req dto
     */
    protected abstract void updateThreadPoolParameter(ConfigModifyVerifyReqDTO reqDTO);
}
